﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Item;

namespace Player
{

    public class Backpack
    {
        private Dictionary<string, int> dictItemCount;
        public IEnumerable<KeyValuePair<string, int>> AllItem => dictItemCount;

        public event System.Action EventOnItemChange;
        public event System.Action EventOnGoldChange;

        public event System.Action EventOnBombCountChange;

        /*TODO 
         * 作用
         * 1，买金币弹
         * 2，商店
         * 3，加速科研
         */
        private int _gold;
        public int Gold
        {
            get => _gold;
            set
            {
                _gold = value;
                EventOnGoldChange?.Invoke();
            }
        }

        private int _bombCount;
        public int BombCount
        {
            get => _bombCount;
            set
            {
                _bombCount = value;
                EventOnBombCountChange?.Invoke();
            }
        }
        
        public Backpack()
        {
            dictItemCount = new Dictionary<string, int>();
            _gold = 0;
        }

        public void Reset()
        {
            Gold = 0;
            dictItemCount.Clear();
            EventOnItemChange?.Invoke();
        }

        public void GetItem(string itemTag,int count=1)
        {
            if (count <= 0) return;
            if(ItemDataMgr.Ins[itemTag] == null)
            {
                Debug.Log($"没有该道具的信息 tag:{itemTag}");
                return;
            }

            //Debug.Log($"添加道具 tag:{itemTag}");

            if (dictItemCount.ContainsKey(itemTag))
                dictItemCount[itemTag]+=count;
            else
                dictItemCount.Add(itemTag, count);

            EventOnItemChange?.Invoke();
        }
        public void RemoveItem(string itemTag)
        {
            //Debug.Log($"删除道具 tag:{itemTag}");

            int curCount = 0;
            if (dictItemCount.TryGetValue(itemTag, out curCount))
            {
                curCount--;
                if (curCount <= 0)
                    dictItemCount.Remove(itemTag);
                else
                    dictItemCount[itemTag] = curCount;
            }

            EventOnItemChange?.Invoke();
        }
    }

}